﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateTool
{
    public partial class Form_More : Form
    {
        private int updatemethod = 0;
        private Form1 form1;
        //private bool newFlag = false;
        private int paramindex = 0;
        public Form_More(Form1 form1)
        {
            this.form1 = form1;
            InitializeComponent();
            for (int i = 0; i < form1.comboBox_datasource.Length; i++)
            {
                comboBox_UpdateMethod.Items.Add(form1.comboBox_datasource[i]);
            }
            button_Select.Visible = true;
        }

        public Form_More(Form1 form1, string filename, string local, string cloud, bool processcheck, int updatemethod, int index)
        {
            this.form1 = form1;
            InitializeComponent();
            for (int i = 0; i < form1.comboBox_datasource.Length; i++)
            {
                comboBox_UpdateMethod.Items.Add(form1.comboBox_datasource[i]);
            }
            textBox_FileName.Text = filename;
            textBox_Local.Text = local;
            textBox_Cloud.Text = cloud;
            checkBox_ProcessCheck.Checked = processcheck;
            this.updatemethod = updatemethod;
            button_Select.Visible = false;
            paramindex = index;
        }

        private void Form_More_Shown(object sender, EventArgs e)
        {
            int count = comboBox_UpdateMethod.Items.Count;
            if ((count > 0) && (count > updatemethod))
            {
                comboBox_UpdateMethod.SelectedIndex = updatemethod;
            }
        }

        private void button_Select_Click(object sender, EventArgs e)
        {
            //新建一个文件对话框
            OpenFileDialog pOpenFileDialog = new OpenFileDialog();

            //设置对话框标题
            pOpenFileDialog.Title = "打开本机文件";

            //设置打开文件类型
            pOpenFileDialog.Filter = "exe文件（*.exe）|*.exe|dll文件（*.dll）|*.dll|所有文件|*.*";
            pOpenFileDialog.FilterIndex = 4;

            //监测文件是否存在
            pOpenFileDialog.CheckFileExists = true;

            //文件打开后执行以下程序
            if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox_FileName.Text = System.IO.Path.GetFileName(pOpenFileDialog.FileName);//得到文件
                textBox_Local.Text = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);//绝对路径
                //System.IO.Path.GetFullPath(pOpenFileDialog.FileName);                   //绝对路径
                //System.IO.Path.GetExtension(pOpenFileDialog.FileName);                  //文件扩展名
                //System.IO.Path.GetFileNameWithoutExtension(pOpenFileDialog.FileName);   //文件名没有扩展名
                //System.IO.Path.GetFileName(pOpenFileDialog.FileName);                   //得到文件
                //System.IO.Path.GetDirectoryName(pOpenFileDialog.FileName);              //得到路径
            }
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            if (textBox_FileName.Text.Length == 0) return;
            if (!File.Exists(textBox_Local.Text)) return;
            if (button_Select.Visible)//创建一条新的参数
            {
                form1.saveDataGridViewChange(
                    textBox_FileName.Text,
                    textBox_Local.Text,
                    textBox_Cloud.Text,
                    checkBox_ProcessCheck.Checked,
                    comboBox_UpdateMethod.SelectedIndex);
            }
            else
            {
                form1.saveDataGridViewChange(
                    textBox_FileName.Text, 
                    textBox_Local.Text, 
                    textBox_Cloud.Text, 
                    checkBox_ProcessCheck.Checked, 
                    comboBox_UpdateMethod.SelectedIndex, paramindex);
            }
            this.Close();
        }
    }
}
